package com.qfedu.vhr.system.service.impl;

import com.qfedu.vhr.common.RespBean;
import com.qfedu.vhr.system.entity.Department;
import com.qfedu.vhr.system.entity.vo.AddDepartmentVO;
import com.qfedu.vhr.system.entity.vo.DepartmentVO;
import com.qfedu.vhr.system.mapper.DepartmentMapper;
import com.qfedu.vhr.system.service.IDepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.math3.analysis.function.Add;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhangsan
 * @since 2023-01-11
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {

    @Autowired
    DepartmentMapper departmentMapper;

    @Override
    public List<DepartmentVO> getAllDeptWithChildren() {
        return departmentMapper.getDepartmentByParentId(-1);
    }

    @Override
    public RespBean addDepartment(AddDepartmentVO addDepartmentVO) {
        addDepartmentVO.setEnabled(true);
        departmentMapper.addDepartment(addDepartmentVO);
        if (addDepartmentVO.getResult() == 1) {
            //说明添加成功
            return RespBean.ok("添加成功", addDepartmentVO);
        }
        return RespBean.error("添加失败");
    }

    @Override
    public RespBean deleteDeptById(Integer id) {
        AddDepartmentVO d = new AddDepartmentVO();
        d.setId(id);
        departmentMapper.deleteDeptById(d);
        if (d.getResult() == -2) {
            return RespBean.error("该部门下有子部门，删除失败");
        } else if (d.getResult() == -1) {
            return RespBean.error("该部门下有员工，删除失败");
        } else if (d.getResult() == 1) {
            return RespBean.ok("删除成功");
        }
        return RespBean.error("删除失败");
    }

}
